import { queryOneMenu } from "@/api/system/menu";
import { queryOneRole } from "@/api/system/role";
import { queryOneUser } from "@/api/system/user";
import { queryOneDist } from "@/api/system/dist";

const system = {
  state: {
    dialogAddVisible: {
      menu: false,
      role: false,
      user: false,
      dist: false
    },
    dialogUpdateVisible: {
      menu: false,
      role: false,
      user: false,
      dist: false
    },
    oneMenu: {},
    oneRole: {},
    oneUser: {},
    oneDist: {},
    distItemVisible: false
  },
  mutations: {
    SET_ADD_VISIBLE: (state, value) => {
      const key = value[1];
      state.dialogAddVisible[key] = value[0];
    },
    SET_UPDATE_VISIBLE: (state, value) => {
      const key = value[1];
      state.dialogUpdateVisible[key] = value[0];
    },
    SET_ONEMENU: (state, value) => {
      state.oneMenu = value;
    },
    SET_ONEROLE: (state, value) => {
      state.oneRole = value;
    },
    SET_ONEUSER: (state, value) => {
      state.oneUser = value;
    },
    SET_ONEDIST: (state, value) => {
      state.oneDist = value;
    },
    SET_DIST_ITEM_VISIBLE: (state, value) => {
      state.distItemVisible = value;
    },
  },
  actions: {
    SetAddVisible({ commit }, value) {
      commit('SET_ADD_VISIBLE', value);
    },
    SetUpdateVisible({ commit }, value) {
      if (value.length > 2) {
        switch (value[1]) {
          case 'menu':
            queryOneMenu({ _id: value[2] }).then(res => {
              commit('SET_ONEMENU', res.data.data[0]);
              commit('SET_UPDATE_VISIBLE', value);
            })
            break;
          case 'role':
            queryOneRole({ _id: value[2] }).then(res => {
              commit('SET_ONEROLE', res.data.data[0]);
              commit('SET_UPDATE_VISIBLE', value);
            })
            break;
          case 'user':
            queryOneUser({ _id: value[2] }).then(res => {
              commit('SET_ONEUSER', res.data.data[0]);
              commit('SET_UPDATE_VISIBLE', value);
            })
            break;
          case 'dist':
            queryOneDist({ _id: value[2] }).then(res => {
              commit('SET_ONEDIST', res.data.data[0]);
              commit('SET_UPDATE_VISIBLE', value);
            })
            break;
        }
      } else {
        commit('SET_UPDATE_VISIBLE', value);
      }
    },
    SetDistItemVisible({ commit }, value) {
      if (value.length > 1) {
        queryOneDist({ _id: value[1] }).then(res => {
          commit('SET_ONEDIST', res.data.data[0]);
          commit('SET_DIST_ITEM_VISIBLE', value[0]);
        })
      } else {
        commit('SET_DIST_ITEM_VISIBLE', value[0]);
      }
    },
  }
}

export default system;